import {
  getPage
} from '../../utils/config';
import api from '../../utils/api';
import {
  http,
  $toast
} from '../../utils/utils';
import wxApi from '../../utils/wxApi';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    loaddone: false,
    shopabnor: false,
    loadmore: false,
    pageindex: 1,
    pages: 0,
    isVipShow: false
  },
  editcontacter() {
    wxApi.navigateTo(getPage.editcontacter);
  },
  gettel(e) {
    const data = e.currentTarget.dataset || e.target.dataset;
    if (data.id) {
      let {
        userId
      } = this.data;
      http.post(api.default, {
        data: {
          action: 'getloginfo',
          id: data.id,
          userid: userId
        }
      }, false).then((res) => {
        const {
          data
        } = res.data;
        console.log(data);
        wx.makePhoneCall({
          phoneNumber: data //仅为示例，并非真实的电话号码
        })
      });

    }
  },
  gophone(e) {
    console.log('-------', e)
    const data = e.currentTarget.dataset.item || e.target.dataset.item;
    if (data.userid) {
      let {
        userId
      } = this.data;
      http.post(api.default, {
        data: {
          action: 'getvirtualMobile',
          touserid: data.userid,
          userid: userId
        }
      }, false).then((res) => {
        const {
          data
        } = res.data;
        console.log(data);
        wx.makePhoneCall({
          phoneNumber: data //仅为示例，并非真实的电话号码
        })
      });

    }
  },
  /**
   * 收货地址 -- 网路请求
   */
  getlist(cb, a = false) {
    let {
      pages,
      pageindex,
      shopid,
      loaddone
    } = this.data;
    http.post(api.default, {
      data: {
        action: 'getvistorlist',
        shopid: shopid,
        pageindex: pageindex
      }
    }, a).then((res) => {
      const {
        data
      } = res.data;
      //是否加载完毕
      if (pages == data.pages) loaddone = true;
      this.setData({
        pages: data.pages,
        loaddone,
        shopabnor: data.pages == 0 ? true : false,
      });
      cb && cb(data);
    }).catch(err=>{
      this.setData({
        errMsg:err.data.errmsg,
        isVipShow:true
      })
    })
  },
  onVipClose(){
    this.setData({
      isVipShow:false
    })
  },
  govip() {
    wxApi.navigateTo(getPage.vip);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {



    //判断是否登录
    app.getLogin().then((res) => {
      console.log(res.data);
      this.setData({
        logged: res.code,
        userId: res.data.user.user_id,
        shopid: res.data.shop.ShopId
      });
      //获取收货地址列表
      this.getlist((res) => {
        console.log(res.list);
        this.setData({
          list: res.list
        });
      });
    }).catch((res) => {
      this.setData({
        logged: res
      });
      this.goLogin();
    });
  },

  // 去聊天
  gochat(e) {
    let {
      item
    } = e.currentTarget.dataset;
    console.log('itemmmmmm', item)
    var {
      logged
    } = this.data;
    if (logged) {
      wxApi.navigateTo(getPage.chat, {
        id: item.shopid,
        touserid: item.userid
      });
    } else {
      this.goLogin();
    }
  },
  /**
   * 跳转登录
   */
  goLogin() {
    wxApi.setStartPage();
    wxApi.navigateTo(getPage.login);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let {
      pageindex,
      pages,
      loadmore
    } = this.data;
    //未请求完且未在加载中
    if (pages > pageindex && !loadmore) {
      pageindex++;
      loadmore = true;

      //显示加载效果
      this.setData({
        pageindex,
        loadmore
      });
      wx.showNavigationBarLoading();

      //请求数据
      this.getlist((res) => {
        let {
          list
        } = this.data;
        loadmore = false;
        this.setData({
          loadmore,
          list: list.concat(res.list),
        });
        //隐藏加载效果
        wx.hideNavigationBarLoading();
      })
    }
  }

})